package daosimp;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

import util1.HibernateUtil;
import modelo.Cursoperiodo;
import modelo.Periodoacademico;
import daos.CursoPeriodoDAO;

public class CursoPeriodoDAOIMP implements CursoPeriodoDAO{

	private Transaction tx;	

	public Transaction getTx() {
		return tx;
	}

	public void setTx(Transaction tx) {
		this.tx = tx;
	}

	@Override
	public List<Cursoperiodo> obtenerCursoxPeriodo() {
		Periodoacademico peracad = new Periodoacademico();
		List<Cursoperiodo> cursosxPeriodo = new ArrayList<Cursoperiodo>();
		try{
			tx = HibernateUtil.session.beginTransaction();
			String hql = "FROM Periodoacademico where fecInicioPeriodoAcademico<=CURRENT_DATE() AND fecFinPeriodoAcademico>=CURRENT_DATE()";
			Query query = HibernateUtil.session.createQuery(hql);
			if (!query.list().isEmpty()){
				peracad = (Periodoacademico) query.list().get(0);
			}
			hql ="FROM Cursoperiodo where idPeriodoAcademico = '"+peracad.getIdPeriodoAcademico()+"'";
			query = HibernateUtil.session.createQuery(hql);
			if (!query.list().isEmpty()){
				cursosxPeriodo = query.list();
			}
			tx.commit();
		} catch (Exception ex){
			tx.rollback();
		} 
		return cursosxPeriodo;
	}

	@Override
	public Cursoperiodo obtenerCursoPeriodo(String id) {
		Cursoperiodo curper = new Cursoperiodo();
		try{
			tx = HibernateUtil.session.beginTransaction();
			String hql = "FROM Cursoperiodo where idCursoPeriodo='"+id+"'";
			Query query = HibernateUtil.session.createQuery(hql);
			if (!query.list().isEmpty()){
				curper = (Cursoperiodo) query.list().get(0);
			}
			tx.commit();
		} catch (Exception ex){
			tx.rollback();
		} 
		return curper;
	}

	
}
